/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import java.math.BigDecimal;
import model.Account;
import model.AccountManager;
import model.CustomerManager;

/**
 *
 * @author Administrator
 */
public class TransferCmd extends AbstractCommand{

    
    @Override
    public boolean execute(Object args) {
        assert(getAmount().compareTo(new BigDecimal(0)) > 0);
        Account fromAccount = AccountManager.getInstance().getAccountByID(getFromAccountID());
        if( fromAccount == null)
            return false;
        
        Account toAccount = AccountManager.getInstance().getAccountByID(getToAccountID());
        if( toAccount == null)
            return false;
        
        
        if(!CustomerManager.getInstance().getCustomerByID(fromAccount.getOwnerID()).getName().equals(fromAccountOwnerName))
            return false;
        
        if(!CustomerManager.getInstance().getCustomerByID(toAccount.getOwnerID()).getName().equals(toAccountOwnerName))
            return false;
        
        
        if( fromAccount.getPwd().equals(fromPwd) )   {     
            if(fromAccount.getOwnerID().equals(fromAccountOwnerID)) {
                if(fromAccount.canTransferTo(toAccount) ) {
                    boolean result = fromAccount.draw(getAmount());
                    if(result)
                        return toAccount.deposit(amount);                   
                }
            }
        }
        return false;
       
    }
    
    private String fromAccountID;
    private String fromPwd;
    private String fromAccountOwnerID;
    private String fromAccountOwnerName;
    private String toAccountID;
    private String toAccountOwnerName;
    private BigDecimal amount;
    /**
     * @return the fromAccountID
     */
    public String getFromAccountID() {
        return fromAccountID;
    }

    /**
     * @param fromAccountID the fromAccountID to set
     */
    public void setFromAccountID(String fromAccountID) {
        this.fromAccountID = fromAccountID;
    }

    /**
     * @return the toAccountID
     */
    public String getToAccountID() {
        return toAccountID;
    }

    /**
     * @param toAccountID the toAccountID to set
     */
    public void setToAccountID(String toAccountID) {
        this.toAccountID = toAccountID;
    }

    /**
     * @return the amount
     */
    public BigDecimal getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    /**
     * @return the fromPwd
     */
    public String getFromPwd() {
        return fromPwd;
    }

    /**
     * @param fromPwd the fromPwd to set
     */
    public void setFromPwd(String fromPwd) {
        this.fromPwd = fromPwd;
    }

    /**
     * @return the fromAccountOwnerID
     */
    public String getFromAccountOwnerID() {
        return fromAccountOwnerID;
    }

    /**
     * @param fromAccountOwnerID the fromAccountOwnerID to set
     */
    public void setFromAccountOwnerID(String fromAccountOwnerID) {
        this.fromAccountOwnerID = fromAccountOwnerID;
    }

    /**
     * @return the fromAccountOwnerName
     */
    public String getFromAccountOwnerName() {
        return fromAccountOwnerName;
    }

    /**
     * @param fromAccountOwnerName the fromAccountOwnerName to set
     */
    public void setFromAccountOwnerName(String fromAccountOwnerName) {
        this.fromAccountOwnerName = fromAccountOwnerName;
    }

    /**
     * @return the toAccountName
     */
    public String getToAccountOwnerName() {
        return toAccountOwnerName;
    }

    /**
     * @param toAccountName the toAccountName to set
     */
    public void setToAccountOwnerName(String toAccountOwnerName) {
        this.toAccountOwnerName = toAccountOwnerName;
    }
}
